<?php
require_once "../../_inc/funct.xml2array.php";
$isReloadable = false;
require_once("../../../pers/inc.config.php");
require_once('../../_inc/inc.session.php');
require_once('../../_inc/funct.html.php');
require_once('../../_inc/funct.tabDati.php');
require_once('../../_inc/class.rglDocGen.php');

$sSql = "SELECT file_caricato, id_set_dati FROM rgl_bilancio where id_bil = ".reqVarInt('id');
$objDB->query($sSql,'readBilOrig');
$acB = $objDB->fetch('readBilOrig');
if (!$acB) {
	die("id.bil. '$idBil' non trovato ");
}
$sSql = "SELECT cod_dato, cod_fonte FROM rgl_def_dato "
	." WHERE id_set_dati = ".$acB['id_set_dati']." AND tipo_tab_dati = 1 ";
$objDB->query($sSql,'raccDatiFonte');
$aCodFonte = array();
while($aX = $objDB->fetch()) {
	if (!isset($aCodFonte[$aX['cod_fonte']]))
		$aCodFonte[$aX['cod_fonte']] = $aX['cod_dato'];
	else 
		$aCodFonte[$aX['cod_fonte']] .= ", ".$aX['cod_dato'];
}
$aXml = xml2array($acB['file_caricato'],array('withAttr'=>true));
//die(x2vPrint($aXml));

$aData = rglVx($aXml,'aXml','s2xResponse',0,'s2xData',0);
$codCCIAA = rglVx($aData,'BalanceSheetIdentification',0,'CoCProvinceAcronym',0)
	.rglVx($aData,'BalanceSheetIdentification',0,'REANo',0);
$ragSoc = rglVx($aData,'BalanceSheetIdentification',0,'CompanyName',0);
$dataRif = rglVx($aData,'BalanceSheetIdentification',0,'ClosingDateOfBalanceSheet',0);

$oDoc = new rglDocGen();
$oDoc->maxCol = 40;
$oDoc->pdfIntestaz = $ragSoc." - Bil.orig. al ".$dataRif;
$oDoc->bottomLeftText = date("d/m/Y H:i:s")." (".$_SERVER['HTTP_HOST'].RGL_URL_ROOT.")";

$oDoc->nuovaRigaNP();
//$aCol = array('cla'=>'lab1','col'=>4,'alg'=>'C','txt'=>'Codice');
//$oDoc->setColNP($aCol);
$aCol = array('cla'=>'lab1','col'=>30,'alg'=>'C','txt'=>'Descrizione');
$oDoc->setColNP($aCol);
$txt = $dataRif;
$aCol = array('cla'=>'lab1','col'=>10,'alg'=>'C','txt'=>$txt);
$oDoc->setColNP($aCol);

$oDoc->primaPag = true;
$oDoc->nuovaRiga();
$aCol = array('cla'=>'tit1','col'=>40,'alg'=>'C','txt'=>'AZIENDA');
$oDoc->setCol($aCol);
$oDoc->nuovaRiga();
$aCol = array('cla'=>'lab1','col'=>6,'alg'=>'L','txt'=>'Ragione sociale');
$oDoc->setCol($aCol);
$aCol = array('cla'=>'txt1','col'=>26,'alg'=>'L','txt'=>$ragSoc);
$oDoc->setCol($aCol);
$aCol = array('cla'=>'lab1','col'=>4,'alg'=>'L','txt'=>'Cod.CCIAA');
$oDoc->setCol($aCol);
$aCol = array('cla'=>'txt1','col'=>4,'alg'=>'L','txt'=>$codCCIAA);
$oDoc->setCol($aCol);
$oDoc->nuovaRiga();
$aCol = array('cla'=>'lab1','col'=>6,'alg'=>'L','txt'=>'Residenza');
$oDoc->setCol($aCol);
$aCol = array('cla'=>'txt1','col'=>26,'alg'=>'L'
	,'txt'=>rglVx($aData,'BalanceSheetIdentification',0,'Residence',0,'Municipality',0));
$oDoc->setCol($aCol);
$aCol = array('cla'=>'lab1','col'=>4,'alg'=>'L','txt'=>'Provincia');
$oDoc->setCol($aCol);
$aCol = array('cla'=>'txt1','col'=>4,'alg'=>'L'
	,'txt'=>rglVx($aData,'BalanceSheetIdentification',0,'Residence',0,'Province',0));
$oDoc->setCol($aCol);
$oDoc->nuovaRiga();
$aCol = array('cla'=>'lab1','col'=>6,'alg'=>'L','txt'=>'ATECO');
$oDoc->setCol($aCol);
$aCol = array('cla'=>'txt1','col'=>4,'alg'=>'L'
	,'txt'=>rglVx($aData,'BalanceSheetIdentification',0,'AtecoCode02',0));
$oDoc->setCol($aCol);
$aCol = array('cla'=>'lab1','col'=>4,'alg'=>'L','txt'=>'Attivita');
$oDoc->setCol($aCol);
$aCol = array('cla'=>'txt1','col'=>26,'alg'=>'L'
	,'txt'=>rglVx($aData,'BalanceSheetIdentification',0,'AtecoCode02','0A','Description'));
$oDoc->setCol($aCol);

$oDoc->nuovaRiga();
$aCol = array('cla'=>'tit1','col'=>40,'alg'=>'C','txt'=>'Dati riassuntivi');
$oDoc->setCol($aCol);
$aXXX = rglVx($aData,'BalanceSheetIdentification',0,'SectionSummaryItems',0,'SummaryItems');
foreach($aXXX as $kX => $aX) {
	$oDoc->nuovaRiga();
	$oDoc->setParRiga('labNP',true);
	$txt = rglVx($aX,'SummaryItemCode',0);
	$aCol = array('cla'=>'txt1','col'=>3,'alg'=>'L','txt'=>$txt);
	$oDoc->setCol($aCol);
	$txt = str_repeat(' ',rglVx($aX,'Tabulation',0)*2).rglVx($aX,'SummaryItemCode','0A','Description');
	$aCol = array('cla'=>'txt1','col'=>27,'alg'=>'L','txt'=>$txt);
	$oDoc->setCol($aCol);
	$valN = rglVx($aX,'Amount',0)/100;
	$valX = ($valN==0)?'':number_format($valN,2,',','.');
	$aCol = array('cla'=>'txt1','col'=>10,'alg'=>'R','txt'=>$valX);
	$oDoc->setCol($aCol);
}

$oDoc->nuovaRiga(-1);
$aCol = array('cla'=>'tit1','col'=>40,'alg'=>'C','txt'=>'Bilancio');
$oDoc->setCol($aCol);
$aXXX = rglVx($aData,'BalanceSheetIdentification',0,'SectionAccountStatement',0,'AccountStatement');
foreach($aXXX as $kX => $aX) {
	$oDoc->nuovaRiga();
	$oDoc->setParRiga('labNP',true);
	$codFonte = rglVx($aX,'ItemCode',0);
	$aCol = array('cla'=>'txt1','col'=>3,'alg'=>'L','txt'=>$codFonte);
	$oDoc->setCol($aCol);
	$txt = str_repeat(' ',rglVx($aX,'Tabulation',0)*2).rglVx($aX,'ItemCode','0A','Description');
	$aCol = array('cla'=>'txt1','col'=>27,'alg'=>'L','txt'=>$txt);
	$oDoc->setCol($aCol);
	$valN = rglVx($aX,'Amount',0)/100;
	$valX = ($valN==0)?'':number_format($valN,2,',','.');
	$aCol = array('cla'=>'txt1','col'=>6,'alg'=>'R','txt'=>$valX);
	$oDoc->setCol($aCol);
	$txt = rglVx($aCodFonte,$codFonte);
	$aCol = array('cla'=>'txt1','col'=>4,'alg'=>'R','txt'=>$txt);
	$oDoc->setCol($aCol);
}

$oDoc->nuovaRiga(-1);
$aCol = array('cla'=>'tit1','col'=>40,'alg'=>'C','txt'=>'Indici');
$oDoc->setCol($aCol);
$aXXX = rglVx($aData,'BalanceSheetIdentification',0,'SectionRatios',0,'Ratios');
foreach($aXXX as $kX => $aX) {
	$oDoc->nuovaRiga();
	$oDoc->setParRiga('labNP',true);
	$txt = rglVx($aX,'RatioCode',0);
	$aCol = array('cla'=>'txt1','col'=>3,'alg'=>'L','txt'=>$txt);
	$oDoc->setCol($aCol);
	$txt = str_repeat(' ',rglVx($aX,'Tabulation',0)*2).rglVx($aX,'RatioCode','0A','Description');
	$aCol = array('cla'=>'txt1','col'=>12,'alg'=>'L','txt'=>rtrim($txt));
	$oDoc->setCol($aCol);
	$valN = rglVx($aX,'Value',0)/100;
	$valX = ($valN==0)?'':number_format($valN,2,',','.');
	if (rglVx($aX,'ValueRatio',0)=='P') $valX .='%';
	$aCol = array('cla'=>'txt1','col'=>5,'alg'=>'R','txt'=>$valX);
	$oDoc->setCol($aCol);
	$txt = rglVx($aX,'MethodOfCalculationIndicator',0);
	$aCol = array('cla'=>'txt1','col'=>20,'alg'=>'L','txt'=>rtrim($txt));
	$oDoc->setCol($aCol);
}

$sPdf = $oDoc->genPdf(array('orientaPagina'=>'P'));
headerDownLoad(strlen($sPdf),"bilancio_".$codCCIAA."_".$dataRif.".pdf");
echo $sPdf;
exit();

